package com.amit.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class LoginAction  extends Action
{
	
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		
		String result=" ";
		
		//1.collect the data
		
		LoginForm lf=(LoginForm) form;
		
		String un=lf.getUsername();
		String pw=lf.getPassword();
		
		//2.contact model
		if(un.equals(pw))
		{
			result="success";
			request.setAttribute("UN", un);
		}
		else
		{
			result="failed";
			ActionErrors errors=new ActionErrors();
			errors.add("login.invalid", new ActionError("error.login.invalid"));
			this.saveErrors(request, errors);
		}
		
		ActionForward forward=mapping.findForward(result);
		
		return forward;
	}
	

}
